<?php
/**********************************************************
Copyright (c) 2011 Wayne State University Library System
Licensed under the MIT License.
Redistributions of files must retain the above copyright notice.

This software is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

MapIt!
http://www.lib.wayne.edu/MapIt!/
http://code.google.com/XXXX
contact: Paul Gallagher, Developer Librarian WSULS
email: paul.gallagher@wayne.edu
***********************************************************/


function CleanString($string)	{
	$string = str_replace("/", "", $string);
	$string = str_replace("\n", "", $string);
	$string = str_replace("\r", "", $string);
	$string = str_replace("&nbsp;", "", $string);
	$string = str_replace("^", "", $string);
	$string = str_replace("n: ", "", $string);
	$string = trim($string);
	return $string;
}

function LCCallToNumber($call, $type="full")	{
	switch ($type) 
	{
		case ($type == "full"):
			return _LCCallToNumberPrefix($call) . '.' . _LCCallToNumberSuffix($call);
		break;
		case ($type == "prefix"):
			return _LCCallToNumberPrefix($call);
		break;
		case ($type == "suffix"):
			return _LCCallToNumberSuffix($call);
		break;
	}
}

function _LCCallToNumberPrefix($call)	{
	//get prefix
	preg_match("([A-z]{3}|[A-z]{2}|[A-z]{1})", $call, $callPrefix); 	

	//convert letters to numbers, concatenate
	$ret = LetterToNumber(substr($callPrefix[0], 0, 1)).
	LetterToNumber(substr($callPrefix[0], 1, 1)).
	LetterToNumber(substr($callPrefix[0], 2, 1));
		
	//check for lenght
		if (strlen($ret) == 4) {
			$ret .= '00';
		}
		elseif (strlen($ret) == 2) {
			$ret .= '0000';
		}
		
	return $ret;
}
	
function _LCCallToNumberSuffix($call) {		
	//remove spaces
	$call = CleanString($call);

	//remove prefix
	preg_match("([A-z]{3}|[A-z]{2}|[A-z]{1})", $call, $callPrefix); 				//get prefix		
	$suffix = substr($call, strlen($callPrefix[0]));								//remove prefix from suffix
	
	preg_match("([0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]{1})", $suffix, $numbers); 		//get the numbers
	$suffix = str_replace(" ", "", $suffix);										//clean whitespace
	
	$count = strlen($numbers[0]);

	switch ($count) 																//normaize the suffix numbers											
	{				
		case ($count == 3):
			$suffix = '0' . $suffix;
			break;
		case ($count == 2):
			$suffix = '00' . $suffix;
			break;
		case ($count == 1):
			$suffix = '000' . $suffix;
			break;
	}
	
	$suffix = str_replace(".", "", $suffix);					//remove periods
	$suffix = LetterToNumber($suffix);							//number conversion

	return $suffix;
}

function LetterToNumber($letter) {
	$letter = strtoupper($letter);
	 $num = str_replace(
			array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',''), array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','00'), $letter );
   
	return $num;
}

function NumberToLetter($number) {
	  $letter = str_replace(
			 array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','00'), array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',''), $number );
   
	return $letter;
}

function unlinkByWildcard($path,$match='*') {
	static $deld = 0, $dsize = 0;
	$dirs = glob($path."*");
	$files = glob($path.$match);
	foreach($files as $file){
		if(is_file($file)){
			$dsize += filesize($file);
			unlink($file);
			$deld++;
   		}
	}
}